package whackEm.V2;


import whackEm.persistent.GameState;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainMenuActivity extends Activity{
	 private boolean running = true; //If the rabbit animation is running 
	 private String TAG = MainMenuActivity.class.getSimpleName(); //tag for logging 
	 protected ImageView rabbitFrame ; //Frame for the rabbit animation
	 
	 // Buttons for the main menu
	 LinearLayout newGameButton ;
	 LinearLayout justWhackEmButton ;
	 LinearLayout howToPlayButton ;
	 LinearLayout highScoresButton ;
	 LinearLayout exitButton ;
	 
	 // Touch Listener for paiting the main_menu_buttons
	 
	 OnTouchListener l;
	 

	 	 
	public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);        	        
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);	            
	        setContentView(R.layout.mainmenu);	        
	      

	           //////////////////////
			   ////Create Buttons////
			   //////////////////////
	        
	        newGameButton = (LinearLayout) findViewById(R.id.NewGame);
	        justWhackEmButton = (LinearLayout) findViewById(R.id.JustWhackEm);
	        howToPlayButton = (LinearLayout) findViewById(R.id.HowToPlay);
	        highScoresButton = (LinearLayout) findViewById(R.id.HighScores);
	        exitButton = (LinearLayout) findViewById(R.id.Exit);
	        
	        l = new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_DOWN)
						v.setBackgroundResource(R.drawable.main_menu_button_background_touched);
					else if (event.getAction() == MotionEvent.ACTION_UP)
						v.setBackgroundResource(R.drawable.main_menu_button_background);
					return false;
				}
		
			};
	        	        

	           /////////////////////
			   ////  Listeners  ////
			   /////////////////////
	        newGameButton.setOnTouchListener(l);
	        justWhackEmButton.setOnTouchListener(l);
	        howToPlayButton.setOnTouchListener(l);
	        howToPlayButton.setOnTouchListener(l);
	        highScoresButton.setOnTouchListener(l);
	        exitButton.setOnTouchListener(l);
	        
	        
	        
			
	        
	    }

	public void onStop() {
		super.onStop();
	} 
	
	public void onResume(){		
		super.onResume();	
        rabbitFrame = (ImageView) findViewById(R.id.rabbitMenu);
        rabbitFrame.setBackgroundResource(R.drawable.rm10014);	     
		//ad = (AnimationDrawable) rabbitMenu.getBackground();
		Log.d(TAG,"onResume");
		running = true;
	}
	
	public void onWindowFocusChanged(boolean hasFocus) {		
		super.onWindowFocusChanged(hasFocus);					
		if (hasFocus)
			new DrawRabbit().execute((Void)null) ;
		else 
			running = false;
		Log.d(TAG,"onFocusChanged");
	}
		
			//////////////////////
			/// On Click block ///
			//////////////////////
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub		
		return super.onTouchEvent(event);
	}
	
	public void onClickNewGame(View v){								
		Intent startJustWhackEmIntent = new Intent(this, WhackEmActivity.class);		
    	startActivity(startJustWhackEmIntent);
	}
	
	public void onClickJustWhackEm(View v){		
		Intent startJustWhackEmIntent = new Intent(this, WhackEmActivity.class);		
    	startActivity(startJustWhackEmIntent);
	}
	
	public void onClickExit(View v){
	//	ad.stop();
		finish();
	}
	
	public void onClickHowToPlay(View v){
	}
	
	public void onClickHighScores(View v){
		//ad.stop();
		String url = "http://www.facebook.com";
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(url));
		startActivity(i);
	}
		
           /////////////////////
		   //// Draw Rabbit ////
		   /////////////////////

	 private class DrawRabbit extends AsyncTask<Void, Integer, Void> {		
		 private int fps = 24;
		 private int frame = 10014;
		 private String drawableName; 
		 @Override
	     protected Void doInBackground(Void... params) {
	    	 while (running){
	    		 try {	
	    			if (frame == 10014)
	    				Thread.sleep(1000);
	    			else 
	    				Thread.sleep(fps);
					if (frame < 10160) { 
						frame++;
						publishProgress(frame);
					}
					else 
						running = false;
					
				} catch (InterruptedException e) { 
					e.printStackTrace();
				}
	    	 }	    		 
	    	 return null;
	     }
		 
	     protected void onProgressUpdate(Integer... frame) {
	    	 drawableName = "rm" + frame[0];
	    	 int res_id = getResources().getIdentifier(drawableName, "drawable", getPackageName());	    	 
	         rabbitFrame.setBackgroundResource(res_id);
	     }	 
	 }
	 	
	
}
